package com.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.test.context.transaction.TransactionConfiguration;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;

import com.persistence.TFootballTeam;
import com.persistence.dao.TeamDao;
import com.service.PlayerService;
import com.service.TeamService;

@Controller
@SessionAttributes
@TransactionConfiguration(transactionManager = "transactionManager", defaultRollback = false)
@Transactional
public class TeamController extends AbstractController {
	
	ApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"applicationContext.xml"});
	TeamService teamDao = (TeamService) context.getBean("teamService");
	PlayerService playerDao = (PlayerService) context.getBean("playerService");
	
	@Override
	@RequestMapping(value = "/saveTeam", method = RequestMethod.POST)
	protected ModelAndView handleRequestInternal(HttpServletRequest req,
			HttpServletResponse res) throws Exception {
		
		String teamName = req.getParameter("teamName");
		TFootballTeam team = new TFootballTeam();
		team.setIdTeam(teamDao.countAll()+1);
		team.setTeamName(teamName);
		
		TFootballTeam team2 = teamDao.insert(team);
//		teamDao.remove(team);
		
		
		
		List<TFootballTeam> list = teamDao.findAll();
		req.setAttribute("teams", list);
		res.sendRedirect("main.htm");
		return new ModelAndView("menu", "teams", list);
	}


}
